package com.doupi.partner.manager;

import com.doupi.partner.model.UserRelation;
import com.doupi.partner.service.AdministratorService;
import com.doupi.partner.service.dto.UserRelationDto;
import com.doupi.partner.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 合伙人关系 manager
 * 
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Component
public class UserRelationManager {
	private final AdministratorService administratorService;

	@Autowired
	public UserRelationManager(AdministratorService administratorService) {
		this.administratorService = administratorService;
	}

	/**
	 * 将List<UserRelation>转为 List<UserRelationDto> lists
	 */
	public List<UserRelation> convertUserRelationListToDtoList(List<UserRelation> records) {
		List<UserRelation> lists = new ArrayList<>();
		for (UserRelation user : records) {
			lists.add(convertUserRelationToDto(user));
		}
		return lists;
	}

	/**
	 * 将UserRelation转为 UserRelationDto
	 */
	public UserRelationDto convertUserRelationToDto(UserRelation user) {
		UserRelationDto userRelationDto = new UserRelationDto();
		ObjectUtils.copyProperties(userRelationDto, user);
		userRelationDto.setBusinessAdministrator(administratorService.selectByPrimaryKey(user.getBusinessAdministratorId()));
		userRelationDto.setPartnerAdministrator(administratorService.selectByPrimaryKey(user.getPartnerAdministratorId()));
		userRelationDto.setSuperiorPartnerAdministrator(administratorService.selectByPrimaryKey(user.getSuperiorPartnerAdministratorId()));
		userRelationDto.setCityAdministrator(administratorService.selectByPrimaryKey(user.getCityAdministratorId()));
		userRelationDto.setSubstationAdministrator(administratorService.selectByPrimaryKey(user.getSuperiorPartnerAdministratorId()));
		return userRelationDto;
	}
}
